﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Data.Repositories;
using mesoBoard.Data;
using System.Text.RegularExpressions;
using System.Xml;
using System.Text;
using mesoBoard.Common;

namespace mesoBoard.Web.Helpers
{
    public static partial class mesoBoardHtmlHelpers
    {
        public static string SyntaxHighlightingScripts(this HtmlHelper html, params SyntaxHighlighting[] langs)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);

            StringBuilder str = new StringBuilder();

            string javascript = "<script language='javascript' src='{0}' />";
            string style = string.Format("<link type='text/css' rel='stylesheet' href='{0}' />", Url.ThemeContent("Content/SyntaxHighlighter/Styles/SyntaxHighlighter.css"));

            str.AppendLine(style);
            str.AppendLine(string.Format(javascript, Url.ThemeContent("Content/SyntaxHighlighter/Scripts/shCore.js")));
            
            foreach (var l in langs)
            {
                string url = string.Format(Url.ThemeContent("Content/SyntaxHighlighter/Scripts/shBrush{0}.js"), l.ToString());
                str.AppendLine(string.Format(javascript, url));
            }

            TagBuilder runScript = new TagBuilder("script");
            runScript.Attributes.Add("language", "javascript");
            runScript.InnerHtml = string.Format("dp.SyntaxHighlighter.ClipboardSwf = '{0}';{1}dp.SyntaxHighlighter.HighlightAll('code');",
                Url.ThemeContent("Content/SyntaxHighlighter/Scripts/clipboard.swf"), Environment.NewLine);

            str.AppendLine(runScript.ToString());

            return str.ToString();
        }

        public static string SyntaxHighlightingScripts(this HtmlHelper html, string commaDelimitedList)
        {
            string[] split = commaDelimitedList.Split(',');

            List<SyntaxHighlighting> langs = new List<SyntaxHighlighting>();

            foreach (string s in split)
            {
                langs.Add((SyntaxHighlighting)Enum.Parse(typeof(SyntaxHighlighting), s));
            }

            return SyntaxHighlightingScripts(html, langs.ToArray());

        }

        public static decimal PercentOfVotes(this HtmlHelper html, int voteCount, int totalVotes)
        {
            if (totalVotes == 0)
                return 0;

            decimal ratio = (decimal)voteCount / (decimal)totalVotes;
            decimal percent = ratio * 100;

            return Math.Round(percent);
        }

        public static string VoteBar(this HtmlHelper html, int voteCount, int totalVotes)
        {
            decimal percent = PercentOfVotes(html, voteCount, totalVotes) / 2;

            TagBuilder tag = new TagBuilder("div");
            tag.AddCssClass("vote_bar");
            tag.InnerHtml = "&nbsp;";
            tag.Attributes.Add("style", "width:" + percent .ToString() + "%");

            return tag.ToString();
        }
    }
}
